package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentRepair;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepRepairOrderService;
import com.mxpioframework.dbconsole.DbConstants;
import com.mxpioframework.dbconsole.model.DataGridWrapper;
import com.mxpioframework.dbconsole.service.DbService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EquipmentUpkeepRepairOrderServiceImpl extends BaseServiceImpl<EquipmentRepair> implements EquipmentUpkeepRepairOrderService {

    @Autowired
    private EquipmentUpkeepRepairOrderService equipmentUpkeepRepairOrderService;

    @Autowired
    private DbService dbService;

//    @Override
//    @Transactional
//    public List<EquipmentRepair> getAgent(Criteria criteria) {
//        List<EquipmentRepair> list = this.list(EquipmentRepair.class, criteria);
//        List<EquipmentRepair> arrayList = new ArrayList<>();
//        for (EquipmentRepair repair : list) {
//            if (StringUtils.equals(repair.getDistributePersonId(), SecurityUtils.getLoginUser().getUsername()) && repair.getRepairStatus().equals(EquipmentEnums.RepairStatus.REPAIR.getCode())){
//                arrayList.add(repair);
//            }
//        }
//        return arrayList;
//    }

    @Override
    public List<Map<String, Object>> getAgent() throws Exception {
        String sql = "SELECT * FROM mb_erp_repair WHERE MONTH(CREATE_TIME) = MONTH(GETDATE()) AND YEAR(CREATE_TIME) = YEAR(GETDATE()) AND repair_status_ = 20";
        DataGridWrapper dataGridWrapper = dbService.queryTableData(DbConstants.DEFAULTDATASOURCE, "", sql, 10000, 1);
        List<Map<String, Object>> data = dataGridWrapper.getTableData();
        return data;
    }

    @Override
    public Integer getAgentNumber() throws Exception {
        String sql = "SELECT * FROM mb_erp_repair WHERE MONTH(CREATE_TIME) = MONTH(GETDATE()) AND YEAR(CREATE_TIME) = YEAR(GETDATE()) AND repair_status_ = 20";
        DataGridWrapper dataGridWrapper = dbService.queryTableData(DbConstants.DEFAULTDATASOURCE, "", sql, 10000, 1);
        int totalCount = dataGridWrapper.getTotalCount();
        if (totalCount <= 0){
            return 0;
        }else {
            return totalCount;
        }
    }

    @Override
    public Integer getAllNumber() throws Exception {
        String sql = "SELECT * FROM mb_erp_repair WHERE MONTH(CREATE_TIME) = MONTH(GETDATE()) AND YEAR(CREATE_TIME) = YEAR(GETDATE())";
        DataGridWrapper dataGridWrapper = dbService.queryTableData(DbConstants.DEFAULTDATASOURCE, "", sql, 10000, 1);
        int totalCount = dataGridWrapper.getTotalCount();
        if (totalCount <= 0){
            return 0;
        }else {
            return totalCount;
        }
    }
}
